package com.xxx.video.main.utils;

import android.Manifest;
import android.content.pm.PackageManager;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

import com.xxx.video.VideoApplication;
import com.xxx.video.commen.dbs.utils.DbUtils;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.main.beans.AlbumCheckType;
import com.xxx.video.main.beans.LocalMediaFolderInfoBean;
import com.xxx.video.main.beans.LocalMediaInfoBean;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.listeners.UploadFileListener;
import com.xxx.video.network.utils.NetWorkUtils;

import java.io.File;
import java.util.ArrayList;

/**
 *
 */
public class FilePoolManager {

    public ArrayList<LocalMediaInfoBean> mLocalMediaInfoBeanList;

    private static FilePoolManager mFilePoolManager = null;

    private FilePoolManager() {
    }

    public static FilePoolManager getInstance() {
        if (mFilePoolManager == null) {
            synchronized (FilePoolManager.class) {
                if (mFilePoolManager == null) {
                    mFilePoolManager = new FilePoolManager();
                }
            }
        }
        return mFilePoolManager;
    }

    public ArrayList<LocalMediaInfoBean> getFileList() {
        return mLocalMediaInfoBeanList;
    }

    public void startGetFile(CommonResponseListener commonResponseListener) {
        FragmentActivity fragmentActivity = null;
        try {
            fragmentActivity = (FragmentActivity) VideoApplication.getTopActivity();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (fragmentActivity == null) {
            return;
        }
        if (ContextCompat.checkSelfPermission(VideoApplication.getInstance(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            LocalMediaUtils.getInstance().loadAllLocalMediaData(fragmentActivity, AlbumCheckType.ONLY_IMAGE, new OnLoadLocalMediaListener() {
                @Override
                public void onLoadError() {
                    if (commonResponseListener != null) {
                        commonResponseListener.onFailure(null);
                    }
                }

                @Override
                public void onLoadFinish(ArrayList<LocalMediaFolderInfoBean> localMediaFolderInfoBeanList) {
                    if (!ListUtils.isEmpty(localMediaFolderInfoBeanList)) {
                        mLocalMediaInfoBeanList = localMediaFolderInfoBeanList.get(0).mLocalMediaInfoBeanList;
                    }
                    if (commonResponseListener != null) {
                        commonResponseListener.onSuccess(null,"");
                    }
                }
            });
        }
    }

}
